﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;

namespace Poligono.Core
{
    /// <summary>
    /// Manager Thread is wrap to help work with thread
    /// </summary>
    /// <typeparam name="T">Message object <remarks>String type is a simple object type to make message</remarks></typeparam>
    [Serializable]
    public abstract class ManagerThread<T>
    {
        private Guid id;
        Queue<T> messageQueue;

        /// <summary>        
        /// Make a chanel between Thread a "Process Monitor" to know info in execution.
        /// Enqueue Add message (include in list)
        /// </summary>
        /// <remarks>MessageQueueEnqueue is thread-safe</remarks>
        /// <param name="msn">Info ADD</param>
        public void MessageQueueEnqueue(T msn)
        {
            lock (messageQueue)
            {
                messageQueue.Enqueue(msn);
            }
        }

        /// <summary>        
        /// Make a chanel between Thread a "Process Monitor" to know info in execution.
        /// Dequeue Get message (remove of list)
        /// </summary>
        /// <remarks>MessageQueueDequeue is thread-safe</remarks>        
        public T MessageQueueDequeue()
        {
            lock (messageQueue)
            {
                return messageQueue.Dequeue();
            }
        }

        /// <summary>        
        /// Make a chanel between Thread a "Process Monitor" to know info in execution.
        /// Peek Get message without remove of list
        /// </summary>
        /// <remarks>MessageQueueDequeue is thread-safe</remarks>   
        public T MessageQueuePeek()
        {
            lock (messageQueue)
            {
                return messageQueue.Peek();
            }
        }






        /// <summary>
        /// identify the ManagerThread
        /// </summary>
        public Guid Id
        {
            get { return id; }
        }

        /// <summary>
        /// constrtucto of class
        /// </summary>
        /// <param name="id">id to indentify class</param>
        /// <param name="msn">init message, normaly inform Process is Running!!</param>
        public ManagerThread(Guid id, T msn)
        {
            this.id = id;
            messageQueue = new Queue<T>();
            messageQueue.Enqueue(msn);
        }
        /// <summary>
        /// The Method will be call to start process
        /// </summary>
        public abstract void Start();

        /// <summary>
        /// The Method will be call to start process with start info
        /// </summary>
        /// <param name="msn"></param>
        public abstract void Start(T msn);

    }
}
